﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour
{
    GameObject go = null;
    int maxAmount = 0;
    List<GameObject> objectList = new List<GameObject>();

    public ObjectPool(int maxAmount, GameObject go)
    {
        this.go = go;
        this.maxAmount = maxAmount;
        for (int i = 0; i < maxAmount; i++)
        {
            objectList.Add(Instantiate(go));
            objectList[i].SetActive(false);
        }
    }

    public void UseObject()
    {
        foreach (var item in objectList)
        {
            if (item.activeSelf == false)
            {
                item.SetActive(true);
                return;
            }
        }
        AddObject();
    }

    void AddObject()
    {
        objectList.Add(Instantiate(go));
        maxAmount++;
    }
}
